package com.greenantmall.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.greenantmall.R;
import com.greenantmall.activity.MyMsgActivity;
import com.greenantmall.bean.MyMsg;
import com.greenantmall.utils.SQLiteHelper;
import com.greenantmall.view.CommonDialog;
import com.lidroid.xutils.BitmapUtils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyMsgListAdapter extends BaseAdapter {

	private List<MyMsg> list;
	private Context context;
	//BitmapUtils bitmapUtils;
	SimpleDateFormat sd;

	public MyMsgListAdapter(List<MyMsg> list, Context ctx) {
		this.list = list;
		this.context = ctx;
//		bitmapUtils = new BitmapUtils(ctx, ctx.getCacheDir().getAbsolutePath());
//		bitmapUtils.configDiskCacheEnabled(true);
//		bitmapUtils.configMemoryCacheEnabled(true);
//		bitmapUtils.configDefaultLoadingImage(R.drawable.pictures_no);
//		bitmapUtils.configThreadPoolSize(3);
		sd = new SimpleDateFormat("yy-MM-dd HH:mm");

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.mymsg_item, null);
			viewHolder = new ViewHolder();

			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.title = (TextView) convertView.findViewById(R.id.title);
			viewHolder.time = (TextView) convertView.findViewById(R.id.time);
			viewHolder.isread = (ImageView) convertView
					.findViewById(R.id.isread);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final MyMsg goods = list.get(position);
		viewHolder.name.setText(goods.getMsg());

		if (goods.isRead()) {
			viewHolder.isread.setBackgroundResource(R.drawable.icon_msg_readed);
		} else {
			viewHolder.isread.setBackgroundResource(R.drawable.icon_speaker);
		}
		if (goods.getMsgType() == MyMsg.MSG_TYPE_ORDERDETAIL) {
			viewHolder.title.setText(R.string.msg_title_order);
		} else {
			viewHolder.title.setText(R.string.msg_title_system);
		}
		Date date = new Date();
		date.setTime(goods.getTime());
		String ss = sd.format(date);
		viewHolder.time.setText(ss);
		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String msg = list.get(position).getMsg();
				SQLiteHelper.getInstance(context).updateMsgReaded(msg);
				goods.setRead(true);
				notifyDataSetChanged();

			}
		});

		convertView.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {

				CommonDialog errDialog = new CommonDialog(context,
						R.style.ExitDialogStyle, context
								.getString(R.string.common_title), context
								.getString(R.string.delete_hint));
				errDialog.setSureListener(new CommonDialog.OnSureClickListener() {

					@Override
					public void sureClicked() {
						SQLiteHelper.getInstance(context).deleteMsg(
								list.get(position).getMsg());
						list.remove(position);
						notifyDataSetChanged();
						if (list.size() == 0) {
							((MyMsgActivity) context).refreshView();
						}
					}
				});
				errDialog.show();
				return true;
			}
		});
		return convertView;
	}

	class ViewHolder {

		public TextView name;
		public TextView title;
		public TextView time;
		public ImageView isread;

	}

}
